package com.luo.outpatient3.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.luo.outpatient3.entity.Patient;
import com.luo.outpatient3.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * @author 沐辰
 */
@Controller
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    PatientService patientService;
    @PostMapping("/add")
    public String addPatient(String name,
                             String sex,
                             @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate birthday,
                             String tel){
        Patient patient = new Patient();
        patient.setBirthday(birthday);
        patient.setName(name);
        patient.setPhone(tel);
        patient.setSex(sex);
        patientService.save(patient);
        return "redirect:/doctor/patient";
    }
    @PostMapping("/edit")
    public String editPatient(int id,String name,
                             String sex,
                             @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate birthday,
                             String tel){
        Patient patient = new Patient();
        patient.setId(id);
        patient.setBirthday(birthday);
        patient.setName(name);
        patient.setPhone(tel);
        patient.setSex(sex);
        patientService.updateById(patient);
        return "redirect:/doctor/patient";
    }
    @PostMapping("/find")
    @ResponseBody
    private Patient findOne(int id){
        Patient patient = patientService.getById(id);
        return patient;
    }
    @GetMapping("/del")
    private String delPatient(int id){
        patientService.removeById(id);
        return "redirect:/doctor/patient";
    }
    @PostMapping("/delPatients")
    @ResponseBody
    private String delPatients(String str){
        String[] ids = str.split(",");
        for(int i = 0;i<ids.length;i++)
            patientService.removeById(ids[i]);
        return "1";
    }
}
